package com.wangjw.springbootdemo.controller;

import com.wangjw.springbootdemo.pojo.IMoocJSONResult;
import com.wangjw.springbootdemo.pojo.SysUser;
import com.wangjw.springbootdemo.service.IUserService;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * Created by wangjw on 18/5/29.
 */
@RequestMapping("sysUser")
@RestController
public class SysUserController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private Sid sid;

    @RequestMapping("/save")
    public IMoocJSONResult saveUser() throws Exception {
        SysUser user = new SysUser();
        user.setId(sid.nextShort());
        user.setUsername("wang" + new Date());
        user.setNickname("wang" + new Date());
        user.setPassword("abc123");
        user.setIsDelete(0);
        user.setRegistTime(new Date());

        iUserService.saveUser(user);
        return IMoocJSONResult.ok("保存成功");
    }

    @RequestMapping("/update")
    public IMoocJSONResult updateUser() {
        SysUser user = new SysUser();
        user.setId("180529H3F5K4ZSCH");
        user.setUsername("ni" + new Date());
        user.setNickname("ni" + new Date());
        user.setPassword("abc456");

        iUserService.updateUser(user);
        return IMoocJSONResult.ok("修改成功");
    }

    @RequestMapping("/queryListPaged")
    public IMoocJSONResult queryListPaged(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<SysUser> userList = iUserService.queryListPaged(page, pageSize);
        return IMoocJSONResult.ok(userList);
    }

    @RequestMapping("/queryUserById")
    public IMoocJSONResult queryUserById(String userId) {
        SysUser user = iUserService.queryUserById(userId);
        return IMoocJSONResult.ok(user);
    }

    @RequestMapping("/saveTransactional")
    public IMoocJSONResult saveUserTransactional() throws Exception {
        SysUser user = new SysUser();
        user.setId(sid.nextShort());
        user.setUsername("wang" + new Date());
        user.setNickname("wang" + new Date());
        user.setPassword("abc123");
        user.setIsDelete(0);
        user.setRegistTime(new Date());

        iUserService.saveUserTransactional(user);
        return IMoocJSONResult.ok("保存成功");
    }
}
